package hxg.com;

import java.util.concurrent.*;

/**
 * @author HeXiangGuang
 * @create 2021-03-31 22:52
 */
public class CallableTestOptimize {
    public static void main(String[] args)  throws Exception{
        //创建线程池
        ExecutorService es = Executors.newFixedThreadPool(1);

        Future<Integer> future = es.submit(new Callable<Integer>() {

            @Override
            public Integer call() throws Exception {

                System.out.println(Thread.currentThread().getName()+"开始计算");
                int sum = 0;
                for (int i = 0; i < 100; i++) {
                    //添加休眠
                    Thread.sleep(10);
                    sum+=i;
                }
                return sum;
            }
        });
        //获取任务结果
        System.out.println(future.get());
        //关闭线程池
        es.shutdown();
    }
}
